package com.tianhua.javs.core.func.translate.bo;

import com.tianhua.javs.client.bean.JavsParamBean;
import com.tianhua.javs.client.bean.JavsSegmentBean;
import com.tianhua.javs.client.func.IBusinessTranslatorFunc;
import com.tianhua.javs.client.func.ITranslatorFunc;
import com.tianhua.javs.client.model.MethodModel;
import com.tianhua.javs.core.annotations.Segment;
import com.tianhua.javs.core.constant.TagConstants;
import com.tianhua.javs.core.func.translate.AbstractTranslator;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * Description: bo类的转换信息 转换到do
 * date: 2022/2/17
 *
 * @author shenshuai
 * @version 1.0.0
 * @since JDK 1.8
 */
@Component(value = "repositoryFunc")
@Segment(tag = "repository",scriptFuncBean = "boFunc",funcTag = "bo")
public class Repository extends AbstractTranslator implements IBusinessTranslatorFunc {
    @Override
    public JavsSegmentBean translate(String shellSegment, Map<String, JavsParamBean> javsParamBeanMap) {
        String [] segmentArr = shellSegment.trim().split("\\.");
        String segment = segmentArr[0] + TagConstants.POINT + segmentArr[1];
        if (segment.toLowerCase().endsWith("repository")) {
            JavsParamBean javsParamBean = javsParamBeanMap.get(segment);
            JavsParamBean boReferParamBean = javsParamBeanMap.get(segmentArr[0]);

            String javaSegment = shellSegment.replace(segment,javsParamBean.getReferClass().getClassVarIdentifer());
            String methodSegment = segmentArr[segmentArr.length - 1];
            String methodName = methodSegment.split("\\(")[0];
            String segmentClassType = "";
            for (MethodModel methodModel : javsParamBean.getReferClass().getMethodBeanList()){
                if (methodModel.getMethdSimpleName().equals(methodName) ){
                    segmentClassType = methodModel.getReturnClass();
                }
            }

            javaSegment = javaSegment.replace(TagConstants.BRACKET_ALL,TagConstants.BRACKET_LEFT + boReferParamBean.getOriginParamName() + TagConstants.BRACKET_RIGHT);

            return JavsSegmentBean.getMatchInstance(javaSegment, segmentClassType);
        }
        return JavsSegmentBean.getInstance(shellSegment);
    }
}
